<template>
    <div>
        <van-loading v-if="loading" type="spinner" vertical color="#1989fa" class="loading" >
        </van-loading>
        <div v-else class="message">
            <div>{{ message }}</div>
            <van-button v-if="btnShow" type="primary" @click="agentOAuth" class="button">点击重试1</van-button>
        </div>
    </div>
</template>

<script>
// import axios from 'axios'
import { getCookie, setCookie, base64Decode } from '@/utils'
import { wxConfig, agentConfig, getContext } from '@/utils/wxCodeAuth'
import { getAgentInfo } from '@/api/wxconfig'

export default {
    data () {
        return {
            query: {},
            loading: true,
            btnShow: true,
            message: '',
            corpId: '',
            agentId: '',
            pageFlag: '',
            routeName:'',
            callValues: ''
        }
    },
    created () {
        console.log("created,dataInit")
        this.dataInit()
    },
    methods: {
        dataInit () {
            const { callValues, agentId, pageFlag } = this.$route.query
            console.log("redirect From...");
            console.log(this.$route.redirectedFrom)
            console.log("first in pageFlag="+pageFlag)
            const token = getCookie('token')
            if (callValues){
                console.log("entry by callvalues")
                this.loginByCallValues(callValues);
            }else if (agentId){
                console.log("entry by agentId")
                this.agentId = agentId
                setCookie('agentId', agentId)
                if (token){
                    console.log("token is not null")
                    //根据token登录
                    this.loginAndRedirect();
                }else{
                    console.log("oauth by agentId")
                    // oauth2授权成功后会 跳转回调通过callValues登录
                    this.agentOAuth();
                }
            }else{
                console.log("页面错误")
                this.loading = false
                this.btnShow = false
                this.message = '页面参数错误'
            }
        },
        loginByCallValues(callValues){
            const {
                code, msg, data: {
                    act,
                    corpId,
                    agentId,
                    token,
                    expire,
                    sidebarAppName
                }
            } = JSON.parse(base64Decode(callValues))
            console.log("sidebarAppName="+sidebarAppName)
            Object.assign(this, {
                corpId,
                agentId,
                routeName: sidebarAppName,
                pageFlag: act
            })
            if (code == 200 && token && expire) {
                const value = `Bearer ${token}`
                setCookie('token', value, expire)
                setCookie('agentId', agentId, expire)
                setCookie('routeName', sidebarAppName, expire)
                setCookie('corpId', corpId, expire)
                this.loginAndRedirect()
            } else {
                let message
                switch (code) {
                    case 100014:
                        message = '应用的可信域名错误，请检查微信后台配置'
                        break
                    case 100001:
                        message = '授权code码失效'
                        break
                    case 100008:
                        message = '账户异常，无法登录'
                        break
                    case 100013:
                        message = msg
                        break
                    case 100012:
                        message = msg
                        break
                    case 100016:
                        message = msg
                        break
                    default:
                        message = `登录失败请重新登录`
                }
                this.message = message
                this.loading = false
            }
        },
        async loginAndRedirect(){
            const uriPath = this.$route.fullPath
            console.log("login...."+uriPath)
            try {
                await wxConfig(this.corpId, uriPath)
                await agentConfig(this.corpId, uriPath, this.agentId)
                const entry = await getContext()
                this.routeName = getCookie("sidebarAppName")
                console.log("entry="+entry)
                if (entry == 'normal' || entry == 'group_chat_tools') {
                    this.message = '请从个人聊天工具栏进入'
                    this.loading = false
                    this.btnShow = false
                    return
                }
                console.log("loginAndRedirect.routeName="+this.routeName)
                await this.$router.push(this.routeName)
            } catch (e) {
                console.log(e)
                this.loading = false
                this.message = ''
            }
        },
        async agentOAuth(){
            try {
                this.loading = true
                const { data } = await getAgentInfo({ agentId: this.agentId, isJsRedirect: 1, act: this.pageFlag })
                const { url } = data
                if (url) {
                    window.location.href = url
                }
            } catch (e) {
                this.loading = false
                this.message = e
            }
        },
    }
}
</script>

<style lang="less" scoped>
.message,.loading {
    background: #f3f4f8;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 600px;
}
.message {
    flex-direction: column;
    font-size: 30px;
    .button {
        margin-top: 50px;
    }
}

</style>
